package com.webcash.CNBoard.Controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.webcash.CNBoard.Model.Login;
import com.webcash.CNBoard.Model.MyProject;
import com.webcash.CNBoard.Model.ProjectFile;
import com.webcash.CNBoard.Service.ProjectFileService;


@Controller
@RequestMapping("/project/file")
@SessionAttributes("login")
public class ProjectFileController {
	@Autowired
	MyProject myProject;

	@Autowired
	ProjectFileService projectFileService;

	@RequestMapping
	public String Action(@ModelAttribute Login login, Model model){
		List<ProjectFile> projectfilelist = projectFileService.getProjectFileList(myProject.getProjectNo());
		model.addAttribute("ProjectFileList", projectfilelist);
		return "project/file";
	}

	@RequestMapping("/fileUploadAction")
	public String uploadAction( @ModelAttribute Login login, Model model, HttpServletRequest request) throws IllegalStateException, IOException{
		int count = 0;
		MultipartHttpServletRequest mul = (MultipartHttpServletRequest)request;
		MultipartFile projectfile = mul.getFile("projectfile");
		String originalFilename = String.valueOf(projectfile.getOriginalFilename());
		String fileloadPath = "resources/upload/projectfileupload/";
		String temp = originalFilename;
		String root = request.getSession().getServletContext().getRealPath(fileloadPath);
		File fileName = new File(root);
		if(!fileName.exists()){
			fileName.mkdir();
		}
		fileName = new File(root, originalFilename);
		while(fileName.exists()){
			originalFilename = temp;
			++count;
			originalFilename = "("+ count +")"+originalFilename;
			fileName = new File(root, originalFilename);
		}

		projectfile.transferTo(fileName);
		ProjectFile file = new ProjectFile();
		file.setFileName(originalFilename);
		file.setFileSize(String.valueOf(projectfile.getSize()));
		file.setFilePath(fileloadPath+originalFilename);
		file.setProjectfile(projectfile);
		file.setUploadDate(new Date());
		file.setProjectNo(myProject.getProjectNo());
		if(file.getFileName()!=null){
			projectFileService.addProjectFilebyProjectFile(file);
		}
		return "redirect:/project";
	}
	
	@RequestMapping("/deleteprojectaction")
	@ResponseBody
	public Map deleteFileAction(HttpServletRequest request, @ModelAttribute ProjectFile projectfile, @ModelAttribute Login login, Model model)  throws IllegalStateException, IOException
	{
		String fileroot = request.getSession().getServletContext().getRealPath("/resources/upload/projectfileupload/");
		File fileName = new File(fileroot+"/"+projectfile.getFileName());
		fileName.delete();
		projectFileService.removeProjectFilebyFileNo(projectfile);
		List<ProjectFile> projectfilelist = projectFileService.getProjectFileList(myProject.getProjectNo());
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("list", projectfilelist);
		return result;
	}
	
}
